#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.60)
AC_INIT(SkyEye, 1.3.3, skyeye-simulator@googlegroups.com, skyeye)

AC_CANONICAL_SYSTEM
AC_ARG_PROGRAM

AM_INIT_AUTOMAKE
AC_CONFIG_HEADER([config.h])

# some common header path we should include them
COMMON_INCLUDES="-I\$(top_srcdir)/common/include -I\$(srcdir)/common/"
AC_SUBST(COMMON_INCLUDES)
# some common cflags to be used in building all the files.
COMMON_CFLAGS="-Werror-implicit-function-declaration"
AC_SUBST(COMMON_CFLAGS)

AC_CONFIG_MACRO_DIR([m4])

# Checks for programs.
AC_PROG_CXX
AC_PROG_AWK
AC_PROG_CC
AC_PROG_MAKE_SET
AC_PROG_CPP

#LT_PREREQ([2.2.6])
#AC_PROG_LIBTOOL
#AM_PROG_LIBTOOL
#LT_INIT([win32-dll])

AC_PREFIX_DEFAULT([/opt/skyeye])

# according to host type, we can select different macro
dbct=false
case $host in
  i[[3456]]86-*-linux*)
    #dbct=true
    ;;
  *-*-mingw32*)
    CFLAGS="$CFLAGS -mms-bitfields"
    LIBS="$LIBS -lmsvcrt -lws2_32 -lwinmm -ladvapi32"
    #dbct=true
    ;;
  *-*-cygwin*)
    LIBS="$LIBS -L/usr/lib/w32api -lwinmm -ladvapi32"
    #dbct=true
    ;;
  *-*-beos*|*-*-haiku*)
    # TODO
    ;;
esac

# check GCC & DBCT
if test "$GCC" = "yes" ; then
  AC_MSG_CHECKING(for gcc)
  if test -z "$ac_cv_prog_CC" ; then
    gcc_exec="$CC"
  else
    gcc_exec="$ac_cv_prog_CC"
  fi
  case `$gcc_exec -dumpversion | sed -e 's,\..*,.,' -e q` in
    3.*)
      ;;
    [[456789]].*)
      dbct=false
      ;;
    *)
      AC_MSG_ERROR([**** gcc < 3.0 !!!])
      ;;
  esac
  AC_MSG_RESULT([done])
else
  dbct=false
fi
AM_CONDITIONAL([DBCT], [test x$dbct = xtrue])

#check compile target
ENABLE_TARGETS=
AC_ARG_ENABLE([targets],
        AC_HELP_STRING([--enable-targets],[Build specific host targets: all or target1,target2,... Valid targets are: arm) ]),
    [case "${enableval}" in
    arm)ENABLE_TARGETS=arm;;
    all)ENABLE_TARGETS=all;;
    yes)ENABLE_TARGETS=all;;
    *)AC_MSG_ERROR(bad value ${ENABLE_TARGETS} for --enable-targets);;
     esac],
    ENABLE_TARGETS=all)


AM_CONDITIONAL([BUILD_ARM], [test "${ENABLE_TARGETS}" = "arm"])
AM_CONDITIONAL([BUILD_ALL], [test "${ENABLE_TARGETS}" = "all"])

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lc':
AC_CHECK_LIB([c], [main])
# FIXME: Replace `main' with a function in `-liberty':
#AC_CHECK_LIB([iberty], [main])
# FIXME: Replace `main' with a function in `-lintl':
AC_CHECK_LIB([intl], [main])
# FIXME: Replace `main' with a function in `-lm':
AC_CHECK_LIB([m], [main])
# FIXME: Replace `main' with a function in `-ln':
AC_CHECK_LIB([n], [main])

AC_CHECK_LIB([socket], [socket])
AC_CHECK_LIB([pthread], [pthread_create])
AC_CHECK_LIB([ncurses],[tputs])
AC_CHECK_LIB([ltdl],[ltdl])
AC_CHECK_PROG(LLVM,llvm-config,llvm, "no")
AM_CONDITIONAL([LLVM_EXIST], [test x$LLVM = xllvm])
	if test x$LLVM = xllvm ; then
	AC_CACHE_CHECK(for llvm version, version_cv_, llvm-config --version)
	case `llvm-config --version` in
	2.8)
	;;
	*)
	AC_MSG_WARN(llvm should be 2.8 If you want to compile skyeye with other version!)
	AC_MSG_ERROR(Delete me from configuer.in!)
	;;
	esac
	fi

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([fcntl.h inttypes.h limits.h locale.h netdb.h netinet/in.h stddef.h stdint.h stdlib.h string.h sys/file.h sys/ioctl.h sys/socket.h sys/time.h termios.h unistd.h dlfcn.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_INT16_T
AC_TYPE_INT32_T
AC_TYPE_INT64_T
AC_TYPE_INT8_T
AC_TYPE_OFF_T
AC_TYPE_SIZE_T
AC_TYPE_SSIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_TYPE_UID_T
AC_TYPE_UINT16_T
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T
AC_TYPE_UINT8_T
AC_C_VOLATILE

dnl copied from configure.in of bochs-2.4.1

dnl------------ libtool configuration
dnl Configure libtool, and default to shared libraries.  Libtool will only be
dnl used for compiling and linking plugins.
AC_DISABLE_STATIC
dnl Check for dlopen support
AC_LIBTOOL_DLOPEN
dnl Configure libtool
AC_PROG_LIBTOOL
dnl Configure the ltdl library.  This must go after AC_PROG_LIBTOOL or
dnl else it disables shared libraries somehow.  The values from this macro
dnl are written to ltdlconf.h.
dnl AC_LIB_LTDL
#LT_CONFIG_LTDL_DIR([libltdl])
#LTDL_INIT


AC_PATH_XTRA

AC_C_BIGENDIAN
AC_C_INLINE
AC_CHECK_SIZEOF(unsigned char)
AC_CHECK_SIZEOF(unsigned short)
AC_CHECK_SIZEOF(unsigned int)
AC_CHECK_SIZEOF(unsigned long)
AC_CHECK_SIZEOF(unsigned long long)
AC_CHECK_SIZEOF(int *)
AC_CHECK_FUNCS(getenv)
AC_CHECK_FUNCS(setenv)
AC_CHECK_FUNCS(select)
AC_CHECK_FUNCS(snprintf)
AC_CHECK_FUNCS(vsnprintf)
AC_CHECK_FUNCS(strtoull)
AC_CHECK_FUNCS(strtouq)
AC_CHECK_FUNCS(strdup)
AC_CHECK_FUNCS(strrev)
AC_CHECK_FUNCS(stricmp)
AC_CHECK_FUNCS(strcasecmp)
AC_CHECK_FUNCS(sleep)
AC_CHECK_FUNCS(usleep)
AC_CHECK_FUNCS(nanosleep)
AC_CHECK_FUNCS(abort)
AC_CHECK_FUNCS(gettimeofday)
AC_CHECK_TYPE(socklen_t, , ,[#include <sys/types.h> #include <sys/socket.h>])
AC_CHECK_MEMBER(struct sockaddr_in.sin_len, , , [#include <sys/socket.h>
#include <netinet/in.h> ])
AC_CHECK_FUNCS(mkstemp)
AC_CHECK_HEADER(sys/mman.h)
AC_CHECK_FUNCS(timelocal)
AC_CHECK_FUNCS(gmtime)
AC_CHECK_FUNCS(mktime)


AC_CHECK_HEADER(zlib.h)

dnl end of configure.in from bochs

# Checks for library functions.
AC_FUNC_ERROR_AT_LINE
AC_PROG_GCC_TRADITIONAL
AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_FUNC_MMAP
AC_FUNC_REALLOC
AC_FUNC_SELECT_ARGTYPES
AC_TYPE_SIGNAL
AC_FUNC_VPRINTF
AC_CHECK_FUNCS([atexit bzero getpagesize gettimeofday memmove memset munmap select setlocale socket sqrt strcasecmp strchr strdup strerror strspn strtol strtoul])

dnl Check for __attribute__(())
AC_CACHE_CHECK([for __attribute__((packed))],
		  [ac_cv_c_attribute_packed],
		    [ac_cv_c_attribute_packed=no
			   AC_TRY_COMPILE(, [struct __attribute__((__packed__)) foo { int a; } b;],
				                       [ac_cv_c_attribute_packed=yes])])
if test "${ac_cv_c_attribute_packed}" != "no"; then
  AC_DEFINE(HAVE_ATTRIBUTE_PACKED, 1, Support for __attribute__((packed)) for structs)
fi

AH_TEMPLATE([HAVE_ATTRIBUTE_ALIGNED], [Has __attribute__((aligned))])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[]],
				        [[char testchar[4] __attribute__((aligned));]])],
			        [AC_DEFINE(HAVE_ATTRIBUTE_ALIGNED, 1)],
			        [])
# librt
AC_CHECK_LIB(rt,sem_init,[have_librt=yes],[have_librt=no])
if test "${have_librt}" != "no"; then
  AC_DEFINE(HAVE_LIBRT, 1, Check LIBRT)
fi

AC_CONFIG_FILES(Makefile)
AC_CONFIG_FILES(device/Makefile)
AC_CONFIG_FILES(common/Makefile)
AC_CONFIG_FILES(soc/arm/Makefile)

if test ["${ENABLE_TARGETS}" = "all"] ;then
    AC_CONFIG_FILES(arch/arm/Makefile)
else
    AC_CONFIG_FILES(arch/${ENABLE_TARGETS}/Makefile)
fi
AC_OUTPUT 

